import Mock from 'mockjs'
import useRequest from "../../packages/useRequest"

function getUserName(): Promise<string> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (Math.random() > 0.5) {
        resolve(Mock.mock('@name'));
      } else {
        reject(new Error('Failed to get username'));
      }
    }, 1000)
  })
}

export default function Demo1() {
  const { data, error, loading } = useRequest(getUserName)

  if (loading) {
    return <div>loading...</div>
  }
  if(error){
    return <div>加载失败</div>
  }
  return <div>
    加载到名字是：{data}
  </div>
}